﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace InventoryManager
{
    public partial class ViewOrderForm_1_UserControl : UserControl
    {
        public event FormChangedHandler FormChangedEvent;

        private string _connectionString;
        private MySqlConnection _connection;
        private MySqlDataAdapter _mySqlDataAdapter;

        private const string COLUMN_1_NAME = "เลขที่จากบริษัท";
        private const string COLUMN_2_NAME = "เลขที่จากผู้ขาย";
        private const string COLUMN_3_NAME = "วันที่ออกโดยบริษัท";
        private const string COLUMN_4_NAME = "วันที่ออกโดยผู้ขาย";
        private const string COLUMN_5_NAME = "ผู้ขาย";

        public ViewOrderForm_1_UserControl(string connectionString, int width, int height)
        {
            InitializeComponent();

            RecentlyAddedButton_1.SoftButton_1.Click += new EventHandler(SoftButton_1_1_Click);
            RecentlyAddedButton_1.SoftButton_2.Click += new EventHandler(SoftButton_2_1_Click);
            RecentlyAddedButton_2.SoftButton_1.Click += new EventHandler(SoftButton_1_2_Click);
            RecentlyAddedButton_2.SoftButton_2.Click += new EventHandler(SoftButton_2_2_Click);
            RecentlyAddedButton_3.SoftButton_1.Click += new EventHandler(SoftButton_1_3_Click);
            RecentlyAddedButton_3.SoftButton_2.Click += new EventHandler(SoftButton_2_3_Click);
            RecentlyAddedButton_4.SoftButton_1.Click += new EventHandler(SoftButton_1_4_Click);
            RecentlyAddedButton_4.SoftButton_2.Click += new EventHandler(SoftButton_2_4_Click);

            RecentlyAddedButton_1.SoftButton_1.MouseHover += new EventHandler(SoftButton_1_MouseHover);
            RecentlyAddedButton_2.SoftButton_1.MouseHover += new EventHandler(SoftButton_1_MouseHover);
            RecentlyAddedButton_3.SoftButton_1.MouseHover += new EventHandler(SoftButton_1_MouseHover);
            RecentlyAddedButton_4.SoftButton_1.MouseHover += new EventHandler(SoftButton_1_MouseHover);
            RecentlyAddedButton_1.SoftButton_2.MouseHover += new EventHandler(SoftButton_2_MouseHover);
            RecentlyAddedButton_2.SoftButton_2.MouseHover += new EventHandler(SoftButton_2_MouseHover);
            RecentlyAddedButton_3.SoftButton_2.MouseHover += new EventHandler(SoftButton_2_MouseHover);
            RecentlyAddedButton_4.SoftButton_2.MouseHover += new EventHandler(SoftButton_2_MouseHover);

            _connectionString = connectionString;
            this.Width = width;
            this.Height = height;

            LoadRecentlyAddedOrderForms();
        }

        void SoftButton_1_MouseHover(object sender, EventArgs e)
        {
            HelpToolTip.Show("เปิดรายการ", (Button)sender);
        }

        void SoftButton_2_MouseHover(object sender, EventArgs e)
        {
            HelpToolTip.Show("แก้ไขรายการ", (Button)sender);
        }

        void SoftButton_2_1_Click(object sender, EventArgs e)
        {
            EditOrderForm editOrderForm = new EditOrderForm(_connectionString, 
                1, Int32.Parse(RecentlyAddedButton_1.IDLabel.Text));
            editOrderForm.EditFormClosedEvent += new EditFormClosedHandler(editOrderForm_EditFormClosedEvent);
            editOrderForm.ShowDialog();
        }

        void SoftButton_1_1_Click(object sender, EventArgs e)
        {
            OpenReport(1, Int32.Parse(RecentlyAddedButton_1.IDLabel.Text));
        }

        void SoftButton_2_2_Click(object sender, EventArgs e)
        {
            EditOrderForm editOrderForm = new EditOrderForm(_connectionString, 
                1, Int32.Parse(RecentlyAddedButton_2.IDLabel.Text));
            editOrderForm.EditFormClosedEvent += new EditFormClosedHandler(editOrderForm_EditFormClosedEvent);
            editOrderForm.ShowDialog();
        }

        void SoftButton_1_2_Click(object sender, EventArgs e)
        {
            OpenReport(1, Int32.Parse(RecentlyAddedButton_2.IDLabel.Text));
        }

        void SoftButton_2_3_Click(object sender, EventArgs e)
        {
            EditOrderForm editOrderForm = new EditOrderForm(_connectionString,
                1, Int32.Parse(RecentlyAddedButton_3.IDLabel.Text));
            editOrderForm.EditFormClosedEvent += new EditFormClosedHandler(editOrderForm_EditFormClosedEvent);
            editOrderForm.ShowDialog();
        }

        void SoftButton_1_3_Click(object sender, EventArgs e)
        {
            OpenReport(1, Int32.Parse(RecentlyAddedButton_3.IDLabel.Text));
        }

        void SoftButton_2_4_Click(object sender, EventArgs e)
        {
            EditOrderForm editOrderForm = new EditOrderForm(_connectionString,
                1, Int32.Parse(RecentlyAddedButton_4.IDLabel.Text));
            editOrderForm.EditFormClosedEvent += new EditFormClosedHandler(editOrderForm_EditFormClosedEvent);
            editOrderForm.ShowDialog();
        }

        void SoftButton_1_4_Click(object sender, EventArgs e)
        {
            OpenReport(1, Int32.Parse(RecentlyAddedButton_4.IDLabel.Text));
        }

        void editOrderForm_EditFormClosedEvent(int recordType)
        {
            if (recordType == 1)
                FormChangedEvent("ORDERFORM_SELECTION", null);
        }

        private void OpenReport(int reportType, int orderID)
        {
            ShowPriceForm showPriceForm = new ShowPriceForm();
            ShowReportForm showReportForm;

            switch (showPriceForm.ShowDialog())
            {
                case DialogResult.OK:
                    showReportForm = new ShowReportForm(_connectionString);
                    showReportForm.setReportToShow(reportType, orderID, 
                        !showPriceForm.ShowPriceCheckBox.Checked, !showPriceForm.ShowWeightCheckBox.Checked, null);
                    showReportForm.LoadReport();
                    showReportForm.ShowDialog();
                    break;
                default:
                    break;
            }
        }

        private void LoadRecentlyAddedOrderForms()
        {
            try
            {
                _connection = new MySqlConnection(_connectionString);
                _connection.Open();

                string query = "SELECT * from orderidsellername ORDER BY OrderID DESC LIMIT 0, 4";

                _mySqlDataAdapter = new MySqlDataAdapter(query, _connection);

                DataTable dataTable = new DataTable();
                _mySqlDataAdapter.Fill(dataTable);

                int itemsCount = 0;

                RecentlyAddedButton_1.Visible = false;
                RecentlyAddedButton_2.Visible = false;
                RecentlyAddedButton_3.Visible = false;
                RecentlyAddedButton_4.Visible = false;

                DateTime dateTime = DateTime.Now;

                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    int ID;
                    switch (i)
                    {
                        case 0:
                            ID = Int32.Parse(dataTable.Rows[i][0].ToString());
                            RecentlyAddedButton_1.IDLabel.Text = ID.ToString("D6");
                            dateTime = DateTime.Parse(dataTable.Rows[i][2].ToString());
                            RecentlyAddedButton_1.DateCreatedLabel.Text =
                                ThaiWordsGenerator.ToThaiDate(dateTime);
                            RecentlyAddedButton_1.CompName.Text =
                                dataTable.Rows[i][1].ToString();
                            RecentlyAddedButton_1.Visible = true;
                            break;
                        case 1:
                            ID = Int32.Parse(dataTable.Rows[i][0].ToString());
                            RecentlyAddedButton_2.IDLabel.Text = ID.ToString("D6");
                            dateTime = DateTime.Parse(dataTable.Rows[i][2].ToString());
                            RecentlyAddedButton_2.DateCreatedLabel.Text =
                                ThaiWordsGenerator.ToThaiDate(dateTime);
                            RecentlyAddedButton_2.CompName.Text =
                                dataTable.Rows[i][1].ToString();
                            RecentlyAddedButton_2.Visible = true;
                            break;
                        case 2:
                            ID = Int32.Parse(dataTable.Rows[i][0].ToString());
                            RecentlyAddedButton_3.IDLabel.Text = ID.ToString("D6");
                            dateTime = DateTime.Parse(dataTable.Rows[i][2].ToString());
                            RecentlyAddedButton_3.DateCreatedLabel.Text =
                                ThaiWordsGenerator.ToThaiDate(dateTime);
                            RecentlyAddedButton_3.CompName.Text =
                                dataTable.Rows[i][1].ToString();
                            RecentlyAddedButton_3.Visible = true;
                            break;
                        default:
                            ID = Int32.Parse(dataTable.Rows[i][0].ToString());
                            RecentlyAddedButton_4.IDLabel.Text = ID.ToString("D6");
                            dateTime = DateTime.Parse(dataTable.Rows[i][2].ToString());
                            RecentlyAddedButton_4.DateCreatedLabel.Text =
                                ThaiWordsGenerator.ToThaiDate(dateTime);
                            RecentlyAddedButton_4.CompName.Text =
                                dataTable.Rows[i][1].ToString();
                            RecentlyAddedButton_4.Visible = true;
                            break;
                    }
                    itemsCount++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _connection.Close();
            }
        }

        private DataTable LoadDataToDataGrid()
        {
            _connection.Open();

            string fromDate = FromDateTimePicker.Value.ToString("yyyy-MM-dd") + " 00:00:00";
            string toDate = ToDateTimePicker.Value.ToString("yyyy-MM-dd") + " 23:59:59";

            string query = "SELECT * from orderidsellername WHERE Date BETWEEN \'" +
                fromDate + "\' AND \'" + toDate + "\' ORDER BY OrderID DESC";

            _mySqlDataAdapter = new MySqlDataAdapter(query, _connection);

            DataTable srcDataTable = new DataTable();
            _mySqlDataAdapter.Fill(srcDataTable);

            DataTable dstDataTable = new DataTable();

            dstDataTable.Columns.Add(COLUMN_1_NAME);
            dstDataTable.Columns.Add(COLUMN_2_NAME);
            dstDataTable.Columns.Add(COLUMN_3_NAME);
            dstDataTable.Columns.Add(COLUMN_4_NAME);
            dstDataTable.Columns.Add(COLUMN_5_NAME);

            foreach (DataRow dtRow in srcDataTable.Rows)
            {
                DataRow newDtRow = dstDataTable.NewRow();

                int orderID = Int32.Parse(dtRow[0].ToString());
                newDtRow[0] = orderID.ToString("D6");
                newDtRow[1] = dtRow[3].ToString();

                DateTime dateTime = DateTime.Parse(dtRow[2].ToString());
                newDtRow[2] = ThaiWordsGenerator.ToThaiDate(dateTime);

                if (dtRow[4].ToString() == "")
                {
                    newDtRow[3] = null;
                }
                else
                {
                    DateTime sellerDateTime = DateTime.Parse(dtRow[4].ToString());
                    newDtRow[3] = ThaiWordsGenerator.ToThaiDate(sellerDateTime);
                }

                newDtRow[4] = dtRow[1];

                dstDataTable.Rows.Add(newDtRow);
            }

            return dstDataTable;
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dataTable = LoadDataToDataGrid();

                FormChangedEvent("ORDERFORM_VIEW_2", new object[] 
                { dataTable, ThaiWordsGenerator.ToThaiDate(FromDateTimePicker.Value), 
                    ThaiWordsGenerator.ToThaiDate(ToDateTimePicker.Value) });

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _connection.Close();
            }
        }
    }
}
